#!/bin/bash

# 建议service_name多个单词用-分开，全小写
service_name=$1
if [[ -z ${service_name} ]]; then
  echo "sh copy.sh [service_name]"
  exit 1
fi

package_name=${service_name//-/.}
database_name=${service_name//-/_}
dir_name=${service_name//-//}

function copy_module() {
    module_name=$1
    mv scaffold-${module_name} ${service_name}-${module_name}
    rm -rf ${service_name}-${module_name}/target
    sed -i 's/scaffold/'${service_name}'/g' ${service_name}-${module_name}/pom.xml
    if [[ -d "${service_name}-${module_name}/src/main/resources/mapper" ]]; then
      sed -i 's/scaffold/'${package_name}'/g' ${service_name}-${module_name}/src/main/resources/mapper/*.xml
    fi
    find ${service_name}-${module_name}/ -type f -name *.java | xargs sed -i 's/scaffold/'${package_name}'/g'
    if [[ -d "${service_name}-${module_name}/src/main/java/com/earthea/scaffold" ]]; then
      mkdir -p ${service_name}-${module_name}/src/main/java/com/earthea/${dir_name}
      cp -rf ${service_name}-${module_name}/src/main/java/com/earthea/scaffold/* ${service_name}-${module_name}/src/main/java/com/earthea/${dir_name}
      rm -rf ${service_name}-${module_name}/src/main/java/com/earthea/scaffold
    fi
    if [[ -d "${service_name}-${module_name}/src/test/java/com/earthea/scaffold" ]]; then
      mkdir -p ${service_name}-${module_name}/src/test/java/com/earthea/${dir_name}
      cp -rf ${service_name}-${module_name}/src/test/java/com/earthea/scaffold/* ${service_name}-${module_name}/src/test/java/com/earthea/${dir_name}
      rm -rf ${service_name}-${module_name}/src/test/java/com/earthea/scaffold
    fi
}

cd ../
rm -rf ${service_name}
cp -r scaffold ${service_name}
cd ${service_name}
rm -rf .idea .git logs copy.sh scaffold-website/package-lock.json scaffold-website/node_modules
rm -rf docker/data docker/dist docker/.env docker/*.tar.gz
sed -i 's/scaffold/'${service_name}'/g' .gitignore
sed -i 's/scaffold/'${service_name}'/g' pom.xml
sed -i 's/scaffold/'${service_name}'/g' scaffold-website/package.json
sed -i 's/scaffold/'${package_name}'/g' scaffold-web/src/main/resources/application.yml
sed -i 's/scaffold/'${service_name}'/g' docker/*
sed -i 's/scaffold/'${database_name}'/g' docker/volumes/mysql/*

copy_module common
copy_module login
copy_module service

mv scaffold-website ${service_name}-website
cd ${service_name}-website
npm install